unit UFrmBlobField;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Buttons, ComCtrls, ExtDlgs, ATBinHex, ToolWin, ImgList;

type
  TFrmBlobField = class(TForm)
    bvl: TBevel;
    pgc: TPageControl;
    tsText: TTabSheet;
    tsImage: TTabSheet;
    img: TImage;
    dlgSave: TSavePictureDialog;
    dlgFind: TFindDialog;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel1: TPanel;
    GroupBox3: TGroupBox;
    chkModeText: TRadioButton;
    chkModeBinary: TRadioButton;
    chkModeHex: TRadioButton;
    chkModeUnicode: TRadioButton;
    chkWordWrap: TCheckBox;
    pnlButtom: TPanel;
    btnOK: TBitBtn;
    btnCancel: TBitBtn;
    btnExport: TBitBtn;
    edtFind: TEdit;
    lbl1: TLabel;
    SaveDialog1: TSaveDialog;
    ts1: TTabSheet;
    mmoEncode: TMemo;
    tlb1: TToolBar;
    btn1: TToolButton;
    btn2: TToolButton;
    il1: TImageList;
    btn3: TBitBtn;
    dlgOpenPic1: TOpenPictureDialog;
    mmoText: TMemo;
    edtView: TATBinHex;
    procedure imgClick(Sender: TObject);
    procedure imgDblClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnExportClick(Sender: TObject);
    procedure edtFindKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure chkModeTextClick(Sender: TObject);
    procedure chkModeBinaryClick(Sender: TObject);
    procedure chkModeHexClick(Sender: TObject);
    procedure chkModeUnicodeClick(Sender: TObject);
    procedure chkWordWrapClick(Sender: TObject);
    procedure ts1Show(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    FLoad: Boolean;
    procedure UpdateView(en: boolean);
    { Private declarations }
  public
    PictureFormat, TextString: string;
    Stream: TMemoryStream;
  end;

implementation

uses UFrmBigImage, UDecode;

{$R *.dfm}

procedure TFrmBlobField.imgClick(Sender: TObject);
begin
  img.Stretch := not img.Stretch;
end;

procedure TFrmBlobField.imgDblClick(Sender: TObject);
var
  Frm: TFrmBigImage;
begin
  Frm := TFrmBigImage.Create(Self);
  frm.Width := img.Picture.Width;
  frm.Height := img.Picture.Height;
  Frm.img.Picture.Assign(img.Picture);
  Frm.ShowModal;
  frm.Free;
end;

procedure TFrmBlobField.ts1Show(Sender: TObject);
begin
  mmoEncode.Lines.Text := TextString;
end;

procedure TFrmBlobField.btn1Click(Sender: TObject);
begin
  mmoEncode.Lines.Text := EncodeBase64(mmoEncode.Lines.Text);
  mmoText.Lines.Text:=mmoEncode.Lines.Text ;
end;

procedure TFrmBlobField.btn2Click(Sender: TObject);
begin
  mmoEncode.Lines.Text := DecodeBase64(mmoEncode.Text);
end;

procedure TFrmBlobField.btn3Click(Sender: TObject);
begin
//  if PictureFormat = 'TEXT' then
//  begin
//    if OpenTextFileDialog1.Execute then
//      mmoText.Lines.LoadFromFile(OpenTextFileDialog1.FileName);
//  end;
//  if (PictureFormat = 'BMP') or (PictureFormat = 'JPEG') then
//  begin
//    if dlgOpenPic1.Execute then
//      img.Picture.LoadFromFile(dlgOpenPic1.FileName);
//  end;
end;

procedure TFrmBlobField.btnCancelClick(Sender: TObject);
begin
  Close;
end;

procedure TFrmBlobField.btnExportClick(Sender: TObject);
begin
  if (PictureFormat = 'TEXT') and SaveDialog1.Execute then
  begin
    mmoText.Lines.SaveToFile(SaveDialog1.FileName + '.rtf'); ;
    Exit;
  end;
  if dlgSave.Execute then
  begin
    if PictureFormat = 'BMP' then
      img.Picture.SaveToFile(dlgSave.FileName + '.bmp');
    if PictureFormat = 'JPEG' then
      img.Picture.SaveToFile(dlgSave.FileName + '.jpg');
    if Stream <> nil then
    begin
      Stream.Position := 0;
      Stream.SaveToFile(dlgSave.FileName);
    end;
  end;
end;

procedure TFrmBlobField.edtFindKeyPress(Sender: TObject; var Key: Char);
var
//  i: Integer;
  s: string;
begin
  if key = #13 then
  begin
    s := trim(edtFind.text);
    dlgFind.FindText := s;
    if dlgFind.Execute then
    begin
//     frxPreview1.LoadFromFile
    end;
//    for i := 0 to redt.Lines.Count - 1 do
//    begin
//      if pos(s, redt.Lines.Strings[i]) > -1 then
//      begin
//      redt.SelStart
//      end;
//    end;
  end;
end;

procedure TFrmBlobField.FormShow(Sender: TObject);
begin
  if Assigned(Stream) then
  begin
   edtView.OpenStream(Stream);
  end;
  mmoText.Lines.Text := TextString;
end;

procedure TFrmBlobField.chkModeTextClick(Sender: TObject);
begin
  if not FLoad then
  begin
    edtView.Mode := vbmodeText;
    UpdateView(true);
  end;
end;

procedure TFrmBlobField.chkModeBinaryClick(Sender: TObject);
begin
  edtView.Mode := vbmodeBinary;
  UpdateView(true);
end;

procedure TFrmBlobField.chkModeHexClick(Sender: TObject);
begin
  if not FLoad then
  begin
    edtView.Mode := vbmodeHex;
    UpdateView(true);
  end;
end;

procedure TFrmBlobField.chkModeUnicodeClick(Sender: TObject);
begin
  if not FLoad then
  begin
    edtView.Mode := vbmodeUnicode;
    UpdateView(true);
  end;
end;

procedure TFrmBlobField.UpdateView(en: boolean);
begin
  FLoad := true;
  chkModeText.Checked := edtView.Mode = vbmodeText;
  chkModeBinary.Checked := edtView.Mode = vbmodeBinary;
  chkModeHex.Checked := edtView.Mode = vbmodeHex;
  chkModeUnicode.Checked := edtView.Mode = vbmodeUnicode;

  chkModeText.Enabled := en;
  chkModeBinary.Enabled := en;
  chkModeHex.Enabled := en;
  chkModeUnicode.Enabled := en;

  chkWordWrap.Enabled := en and (edtView.Mode in [vbmodeText, vbmodeUnicode]);
//  chkANSI.Enabled := en and (Viewer.Mode <> vbmodeUnicode);
//  chkOEM.Enabled := chkANSI.Enabled;
  FLoad := false;
end;

procedure TFrmBlobField.chkWordWrapClick(Sender: TObject);
begin
  edtView.TextWrap := chkWordWrap.Checked;
end;

end.

